//
//  TestUITableViewframeInUIViewController.m
//  testApp
//
//  Created by iMac on 2018/2/10.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "TestUITableViewframeInUIViewController.h"
#import "WDControllerUtil.h"
#import "AppDelegate.h"
#import "TestRotaTableViewController.h"



#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height
#define navHeight self.navigationController.navigationBar.frame.size.height
#define statusBarHeight [UIApplication sharedApplication].statusBarFrame.size.height

@interface TestUITableViewframeInUIViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *models;
@property (nonatomic, strong) UITableView *tableview;

@end

@implementation TestUITableViewframeInUIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialModels];

    self.title = @"TestUITableViewframeInUIViewController";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = [UIColor orangeColor];
    
//     self.navigationController.navigationBar.hidden = YES;
//     self.tabBarController.tabBar.hidden = YES;
//    [WDControllerUtil setAFullScreenScrollViewFrame:self.tableview controller:self hasStatus:YES hasNav:NO hasTabar:NO];
//
//    self.navigationController.navigationBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = YES;
//    [WDControllerUtil setAFullScreenScrollViewFrame:self.tableview controller:self hasStatus:YES hasNav:YES hasTabar:NO];

//    self.navigationController.navigationBar.hidden = YES;
//    self.tabBarController.tabBar.hidden = NO;
//    [WDControllerUtil setAFullScreenScrollViewFrame:self.tableview controller:self hasStatus:YES hasNav:NO hasTabar:YES];
//
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    [WDControllerUtil setAFullScreenScrollViewFrame:self.tableview controller:self hasStatus:YES hasNav:YES hasTabar:YES];
    
    NSLog(@"nav的高度-%f", self.navigationController.navigationBar.frame.size.height);
    
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = YES;
    
    [self addDismissButton];
}



- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
    }
    return _tableview;
}

- (void)setModels:(NSArray *)models {
    _models = models;
    [self.tableview reloadData];
}
- (void)initialModels {
    NSMutableArray *mArr = [NSMutableArray array];
    for (int i = 0; i < 30; i++) {
        NSArray *objects = [NSArray arrayWithObjects:[NSString stringWithFormat:@"Title - %d", i], [NSString stringWithFormat:@"subtitle - %d", i], nil];
        NSDictionary *dict = [[NSDictionary alloc] initWithObjects:objects forKeys:@[@"title", @"subtitle"]];
        [mArr addObject:dict];
    }
    self.models = mArr.copy;
}


- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"];
    }
    NSDictionary *dic = self.models[indexPath.row];
    cell.textLabel.text = dic[@"title"];
    cell.detailTextLabel.text = dic[@"subtitle"];
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITabBarController *tab = [[UITabBarController alloc] init];
    [tab addChildViewController:[[TestRotaTableViewController alloc] init]];
    [self.navigationController pushViewController:tab animated:true];
}

- (BOOL)shouldAutorotate {
    return YES;
}
//// 这里返回需要支持旋转的方向
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
//    return UIInterfaceOrientationLandscapeLeft;
//}
//// 优先显示的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    UIInterfaceOrientationMask result = [super supportedInterfaceOrientations];
    return result;
}




- (void)addDismissButton {
    UIButton *dismiss = ({
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:@"dismiss" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        CGFloat f = 100;
        btn.frame = CGRectMake(SCREEN_WIDTH - f, SCREEN_HEIGHT - f, f, f);
        [btn setBackgroundColor:[UIColor purpleColor]];
        [btn addTarget:self action:@selector(dismissButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });
    [self.view addSubview:dismiss];
}
- (void)dismissButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:nil];
}


@end
